package com.reds.backend.controller.article;

import com.alibaba.fastjson2.JSONObject;
import com.reds.backend.common.ApiResponse;
import com.reds.backend.pojo.Article;
import com.reds.backend.utils.adminUtils.admin.StpAdminUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.reds.backend.service.article.articleCRUDService;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/article")
public class articleCRUDController {
    @Autowired
        articleCRUDService articleCRUDService;

    @PostMapping("/addCheck/")
    public ApiResponse<String> addCheck(@RequestParam Map<String, String> map){
        StpAdminUtil.checkRoleOr("admin", "superAdmin");
        Article article = JSONObject.parseObject(map.get("article"), Article.class);
        Date date = new Date();
        article.setTime(date);
        return ApiResponse.success(articleCRUDService.addCheck(article, map.get("address")));
    }
    @PostMapping("/addNew/")
    public ApiResponse<Integer> addNew(@RequestParam Map<String, String> map){
        StpAdminUtil.checkRoleOr("admin", "superAdmin");
        Article article = JSONObject.parseObject(map.get("article"), Article.class);
        return ApiResponse.success(articleCRUDService.addNew(article));
    }
    @PostMapping("/detele/")
    public ApiResponse<String> detele(@RequestParam Map<String, String> map){
        StpAdminUtil.checkRoleOr("admin", "superAdmin");
        int id = Integer.parseInt(map.get("id"));
        return ApiResponse.success(articleCRUDService.delete(id));
    }
    @PostMapping("/deteleByTime/")
    public ApiResponse<String> deteleByTime(@RequestParam Map<String, String> map){
        StpAdminUtil.checkRoleOr("admin", "superAdmin");
        return ApiResponse.success(articleCRUDService.deleteByTime(map.get("time")));
    }
    @PostMapping("/updata/")
    public ApiResponse<String> updata(@RequestParam Map<String, String> map){
        StpAdminUtil.checkRoleOr("admin", "superAdmin");
        Article article = JSONObject.parseObject(map.get("article"), Article.class);
        Date date = new Date();
        article.setTime(date);
        return ApiResponse.success(articleCRUDService.update(article));
    }
    @PostMapping("/divByTag/")
    public ApiResponse<List<Article>> search(@RequestParam Map<String, String> map){

        return ApiResponse.success(articleCRUDService.search("1", 1, 1));
    }
    @PostMapping("/test/")
    public ApiResponse<String> test(@RequestParam Map<String, String> map){
        int id = Integer.parseInt(map.get("id"));
        System.out.println(id);
        return ApiResponse.success("success");
    }
    @PostMapping("/setQuickAccess/")
    public ApiResponse<String> setQuickAccess(@RequestParam Map<String, String> map) {
        // 鉴权
        StpAdminUtil.checkRoleOr("admin", "superAdmin");

        if (map.get("id") == null) {
            return ApiResponse.failure(400, "id is required");
        }
        int id = Integer.parseInt(map.get("id"));
        return ApiResponse.success(articleCRUDService.setQuickAccess(id));
    }

    /**
     * 2. 获取所有快速访问的文章
     * 参数: 无 (或者可以加分页参数)
     */
    @PostMapping("/getQuickAccess/")
    public ApiResponse<List<Article>> getQuickAccess(@RequestParam Map<String, String> map) {
        // 获取列表通常不需要管理员权限，如果需要请保留下面这行，不需要则注释掉
        // StpAdminUtil.checkRoleOr("admin", "superAdmin");

        return ApiResponse.success(articleCRUDService.getQuickAccessList());
    }

    /**
     * 3. 移除文章的快速访问标记
     * 参数: id
     */
    @PostMapping("/removeQuickAccess/")
    public ApiResponse<String> removeQuickAccess(@RequestParam Map<String, String> map) {
        // 鉴权
        StpAdminUtil.checkRoleOr("admin", "superAdmin");

        if (map.get("id") == null) {
            return ApiResponse.failure(400, "id is required");
        }
        int id = Integer.parseInt(map.get("id"));
        return ApiResponse.success(articleCRUDService.removeQuickAccess(id));
    }
}
